define(['jquery', 'bootstrap', 'utilback', 'table', 'form', 'myfileinput','zh','ztree','bootstrap-treeview'], function ($, undefined, Utilback, Table, Form,myfileInput,zh,ztree) {

    var Controller = {
    	config: {
    		get_url: '/user/read/detail', // 获取用户详情
    	},
        init: function () {
            // 初始化表格参数配置
            Table.api.init({
            	singleSelect: true,
                extend: {
                    index_url: './json/servicePlan.json', // 查询列表
                    add_url: 'serviceAnnalplanAdd.html', // 新增页面
                    edit_url: 'serviceAnnalplanAdd.html', // 修改页面
                    guidIn_url: 'driverGuidIn.html', //添加导入页面
                    del_url: '/serviceAnnalplan', // 删除
                    multi_url: '/serviceAnnalplan',
                    check_url:'serviceCheck.html',//审核
                    checkRecord_url:'serviceCheckRecord.html',//审核记录
                    checkStart_url:'serviceCheckStart.html',//发起审核
                    info_url:'trackCarRecord.html'
                }
            });
            var table = $("#userTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                method:'get',
                columns: [
                    [
                    	{field: 'state', checkbox: true, 'class': 'hide'},
                        {field: 'id', title: 'ID'},
                        {field: 'Date', title: '年份'},
                        {field: 'carName', title: "车号",formatter : function (value,row,index) {
                            return Table.api.formatter.addtabs(value,row,index,$.fn.bootstrapTable.defaults.extend.info_url);
                        }},
                        {field: 'unit', title: "单位"},
                        {field: 'carType', title: "车辆类型"},
                        {field: 'storagePlace', title: "存放地点"},
                        {field: 'models', title: "规格型号"},
                        {field: 'january', title: "一月"},
                        {field: 'february', title: "二月"},
                        {field: 'march', title: "三月"},
                        {field: 'april', title: "四月"},
                        {field: 'may', title: "五月"},
                        {field: 'june', title: "六月"},
                        {field: 'july', title: "七月"},
                        {field: 'august', title: "八月"},
                        {field: 'september', title: "九月"},
                        {field: 'october', title: "十月"},
                        {field: 'november', title: "十一月"},
                        {field: 'december', title: "十二月"},
                        {field: 'checkState', title: "审核状态"},
                        {field: 'nextCheck', title: "下一审核"},
                        /*{field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index, table);
                            }}*/
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table fa.event.check', function () {
                var ids = Table.api.selectedids(table);
                var audioStatus = Table.api.selectedFields(table, 'checkState');
                var options = table.bootstrapTable('getOptions');
                var toolbar = $(options.toolbar, table);
                $('.btn-del').toggleClass('disabled', true);
                $('.btn-checkStart').toggleClass('disabled', true);
	            $('.btn-check').toggleClass('disabled', true);
                if(audioStatus[0] == '待发起') {
                	$('.btn-del').toggleClass('disabled', false);
                	$('.btn-checkStart').toggleClass('disabled', false);
                } else if(audioStatus[0] == '审核中') {
                   $('.btn-check').toggleClass('disabled', false);
                 }
            });
            // 绑定事件
            $('.btn-check').on('click',function(){
                var ids = Table.api.selectedids(table);
                var checkState = Table.api.selectedFields(table, 'checkState')[0];
                if(checkState != '审核中') {
                	Toastr.error('审核不在同一步骤，请重新选择');
                	return;
                }
                var options = table.bootstrapTable('getOptions');
                var url = options.extend.check_url + (options.extend.check_url.match(/(\?|&)+/)?"&id=" : "?id=") + ids.join(',') + '&t=C';
                Util.api.open(url,__('审核'));
            });
            $('.btn-checkRecord').on('click',function(){
                var ids = Table.api.selectedids(table);
                var options = table.bootstrapTable('getOptions');
                var url = options.extend.checkRecord_url + (options.extend.checkRecord_url.match(/(\?|&)+/)?"&id=" : "?id=") + ids.join(',') + '&t=L';
                Util.api.open(url,__('审核记录'));
            });
            $('.btn-checkStart').on('click',function(){
                var ids = Table.api.selectedids(table);
                var checkState = Table.api.selectedFields(table, 'checkState')[0];
                if(checkState != '待发起') {
                	Toastr.error('请勾选待发起的数据');
                	return;
                }
                var options = table.bootstrapTable('getOptions');
                var url = options.extend.checkStart_url + (options.extend.checkStart_url.match(/(\?|&)+/)?"&id=" : "?id=") + ids.join(',') + '&t=S';
                Util.api.open(url,__('发起审核'));
            });
           },

        edit: function () { // 初始化编辑页面
        	if(Util.api.query('biz')== 'shoes') {
        		$('#cjzr').hide();
        	}
            Form.api.bindevent($("form[role=form]"));
            Form.api.fill($("form[role=form]"),'./json/person.json',1);
             
             var table = $("#checkRecordTable");

            // 初始化表格
            table.bootstrapTable({
                url: './json/person.json',
                method:'get',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'Date', title: '序号'},
                        {field: 'serviceWork', title: "当前步骤名称"},
                        {field: 'carName', title: "办理人"},
                        {field: 'serviceLocation', title: "办理时间"},
                        {field: 'serviceType', title: "状态"},
                        {field: 'serviceSituation', title: "办理意见"},
                        {field: 'operate', title: '下一步办理人', visible: Util.api.query('t')== 'L'}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.chooceMan').on('click',function(){
                var url="serviceCheckMan.html"
                Util.api.open(url,__('复合选择器'));
            });
            
            var defaultData = [
          {
            text: '北京太格',
            href: '#parent1',
            tags: ['2'],
            nodes: [
              {
                text: '路局调度所',
                href: '#child1',
                tags: ['1'],
                nodes: [
                  {
                    text: '调度室',
                    href: '#grandchild1',
                    tags: ['0']
                  }
                ]
              },
              {
                text: '段领导主管',
                href: '#child2',
                tags: ['3'],
                nodes:[
                	{
                		text:'设备科',
                		href:'#grandchild1',
                		tags:['1'],
                		nodes:[
                			{
                				text:'监控分析工区',
                				tags:['0'],
                				href:'nextChild1'
                			}
                		]
                	},
                	{
                		text:'动力设备车间',
                		href:'#grandChild2',
                		tags:['2'],
                		nodes:[
                			{text:'动力设备检修工区',href:'#nextChild1',tags:['0']},
							{text:'动力综合检修工区',href:'#nextChild2',tags:['0']}             			
                		]
                	},{
                		text:'接触网工区',
                		href:'#grandChild3',
                		tags:['0']
                	}
                ]
              }
            ]
          },
        ];
            $('#stree').treeview({
	          color: "#428bca",
	          expandIcon: 'glyphicon glyphicon-chevron-right',
	          collapseIcon: 'glyphicon glyphicon-chevron-down',
	          nodeIcon: 'glyphicon glyphicon-bookmark',
	          data: defaultData
	        });
	         $('#tree2').treeview({
	          color: "#428bca",
	          expandIcon: "glyphicon glyphicon-stop",
	          collapseIcon: "glyphicon glyphicon-unchecked",
	          nodeIcon: "glyphicon glyphicon-user",
	          showTags: true,
	          data: defaultData
	        });
        },
        update: function() {
            Form.api.bindevent($("form[role=form]"));
        	Util.api.ajax({
        		type: 'GET',
        		// url: '/user/read/current'
                url:'',
        	}, function(data) {
        		$('#edit-form').autofill(data);
        	});
        }
    };
    return Controller;
      function changeAgentContent(){
            document.getElementById("inputFileAgent").value
     = document.getElementById("inputFile").value;
    }
});